package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
//@Transactional mongodb的事务必须是复制集的方式
public class StrategyCatalogServiceImpl implements IStrategyCatalogService{
    @Autowired
    private StrategyCatalogRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyService strategyService;


    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);//把id设置为null
        repository.save(strategyCatalog);//存入strategyCatalog对象

    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        repository.save(strategyCatalog);//编辑和保存是一样的,更新之后则要保存strategyCatalog对象

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public StrategyCatalog get(String id) {
        //orEls,查询id,存在就返回,不承诺在就返回null
        return repository.findById(id).orElse(null);

    }

    @Override
    public List<StrategyCatalog> list() {
        //查询所有
        return repository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {

        //分页查询
        //查询对象
        Query query = new Query();
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(template,StrategyCatalog.class,query,pageable);

    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {

        if (StringUtils.hasLength(strategyCatalog.getId())){
            //update
            //特殊案例,页面更新的数据,刚好跟集合中字段个数一致
            //所以这种等价于全量的更新
            this.update(strategyCatalog);
        }else {
            //save
            this.save(strategyCatalog);
        }

    }

    //    <4>攻略的分组下拉框实现
    @Override
    public List<CatalogVO> groupList() {
        //1.需要组装CatalogVOlist对象
        ArrayList<CatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                //group:管道
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        //aggregate:调用这个方法执行操作,得到一个map集合
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();

        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();

            vo.setDestName(data.get("_id").toString());//他通过id获取姓名封装到对vo中

            List<Object> names = (List<Object>) data.get("names");//攻略分类的名称集合
            List<Object> ids = (List<Object>) data.get("ids");//攻略分类的ids集合

            List<StrategyCatalog> catalogList = new ArrayList<>();
            for (int i = 0;i < names.size(); i++) {
                StrategyCatalog sc = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);
            }
            vo.setCatalogList(catalogList);
            //这里的方法和老师的有差别

            list.add(vo);//遍历成功一个就添加到最上面的list集合中
        }
        System.out.println(list);
        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String destId) {

        //1.通过指定destId的所有分类
        List<StrategyCatalog> list = repository.findByDestId(destId);

        //2.遍历分类,查询分类下所有攻略
        for (StrategyCatalog strategyCatalog : list) {
            List<Strategy> sts = strategyService.queryCatalogId(strategyCatalog.getId());
            strategyCatalog.setStrategies(sts);
        }
        return list;
    }

}
